package com.jule.miaozhuapc.webSocket;

import com.jule.miaozhuapc.webSocket.control.WebSocketMessageBean;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.List;

public class  WebSocketSendMessageUtil {

    private Logger logger = Logger.getLogger(WebSocketSendMessageUtil.class);

    public WebSocketMessageBean webSocketMessageBean = new WebSocketMessageBean();
    //排除发送session
    public WebSocketSession excludeSession;
    static {

    }
    /**
     *  游戏消息
     * @param type
     * @param data
     * @param extra
     * @param roomId
     */
    public WebSocketSendMessageUtil(WawaControl type, String data, String extra, Long roomId) {
        if (type != null) {
            this.webSocketMessageBean.setData(data);
            this.webSocketMessageBean.setExtra(extra);
            this.webSocketMessageBean.setRoomId(roomId);
            this.webSocketMessageBean.setType(type.toString());
        }
    }

    public WebSocketSendMessageUtil setData(Object data){
        this.webSocketMessageBean.setData(data);
        return this;
    }
    public WebSocketSendMessageUtil setExTra(String extra){
        this.webSocketMessageBean.setExtra(extra);
        return this;
    }
    public  WebSocketSendMessageUtil setRoomId(Long roomId){
        this.webSocketMessageBean.setRoomId(roomId);
        return this;
    }

    /**
     *  消息类型
     * @param type
     */
    public WebSocketSendMessageUtil(WawaControl type){
        webSocketMessageBean.setType(type.toString());
    }

    /**
     *  排除发送session
     * @return
     */
    public WebSocketSendMessageUtil excludeSession(WebSocketSession session) {
        this.excludeSession = session;
        return this;
    }


    /**
     *  房间列表 使用情况 和 TOAST
     */
//    public WebSocketSendMessageUtil() {
//
//    }

    /**
     *  给当个session 发送消息
     * @param session
     */
    public void sendOne(WebSocketSession session) {
            if(session != null && session.isOpen() && webSocketMessageBean != null){
                TextMessage message = new TextMessage(new JSONObject(webSocketMessageBean).toString());
                try {
                    logger.info("session " + session.getId() +"发送消息 ："+ new JSONObject(webSocketMessageBean).toString() + "成功" );
                    session.sendMessage(message);
                } catch (IOException e) {
                    //e.printStackTrace();
                    logger.info("session " + session.getId() +"发送消息 ："+ new JSONObject(webSocketMessageBean).toString() + "失败" );
                }
            }
    }

    /**
     * 给很多人发送消息
     *
     * @param sessions List<WebSocketSession>
     */
    public void sendList(List<WebSocketSession> sessions) {
        //排除发送人
        if(this.excludeSession != null && sessions != null && sessions.size() > 0)
            sessions.remove(excludeSession);

        if (sessions != null && sessions.size() > 0 &&  this.webSocketMessageBean != null) {
            for (WebSocketSession session : sessions) {
                sendOne(session);
            }
        }
    }


}

